#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using Neatly;
using UnityEngine;

[DisallowMultipleComponent]
public class ManagerSingleton<T> : MonoBehaviour where T : Component
{
    public static T Instance { get; private set; }

    public static void Init()
    {
        if (Instance == null)
        {
            Instance = NeatlyFrame.Instance.gameRoot.AddComponent<T>();
        }
    }

    public virtual void Awake()
    {
        if (Instance != null && Instance.gameObject != gameObject)
        {
            if (Application.isPlaying)
            {
                Destroy(gameObject);
            }
            else
            {
                DestroyImmediate(gameObject);
            }
        }
        else if (Instance == null)
        {
            Instance = GetComponent<T>();
        }
    }

    public static void DestroyInstance()
    {
        if (Instance != null)
        {
            Destroy(Instance.gameObject);
        }
        Instance = null;
    }

    protected virtual void OnDestroy()
    {
        Instance = null;
    }

    public static bool HasInstance()
    {
        return Instance != null;
    }
}